<?php
App::uses('AppController', 'Controller');

class PlanosController extends AppController
{

    public $uses = array('Plano', 'Categoria');
    public $layout = 'epcm';

    public function index()
    {
        $planos = $this->Plano->find('all', array('recursive' => 0));
        $this->set(compact('planos'));
    }

    public function reporte()
    {

        $sql = "SELECT MIN(transmital) as mintran, MAX(transmital) as maxtran, nuevolugar, lugar
                FROM planos
                GROUP BY lugar
                ORDER BY transmital";
        $planos = $this->Plano->query($sql);

        $this->set(compact('planos'));
       // debug($planos);
    }
    
    public function editareporte($idRepor = null)
    {
      $cambiosR = $this->Plano->find('all', array(
            'recursive' => -1,
            'conditions' => array('transmital' => $idRepor),
            'orden' => array('numero ASC')));
        $this->set(compact('cambiosR', 'idRepor'));
        //debug($cambiosR);
                
    }
    
    function insertar()
    {
        $dcc = $this->Categoria->find('list', array('fields' => 'nombre'));
        if (!empty($this->data)) {
            if ($this->Plano->save($this->data)) {
                $this->Session->setFlash('Se Guardo Correctamente!!!');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error al Guardar consulte con el Administrador de Sistema');
            }
        }
        $this->set(compact('dcc'));

    }

    function eliminar($id = null)
    {
        $cambioplano = $this->Plano->find('first', array('recursive' => -1, 'conditions' =>
                array('id' => $id)));
        //debug($cambioplano);exit;
        $cambi = $cambioplano['Plano']['id'];

        $this->Plano->id = $id;
        $this->data = $this->Plano->read();
        if (!$id) {
            $this->Session->setFlash('No existe el Cambio Orden a eliminar');
            $this->redirect(array('action' => 'index'));
        } else {
            if ($this->Plano->delete($id)) {
                $this->Session->setFlash('Se elimino el Plano ' . $this->data['Plano']['nombre']);
                $this->redirect(array(
                    'controller' => 'Planos',
                    'action' => 'index',
                    $cambi));
            } else {
                $this->Session->setFlash('Error al eliminar');
            }
        }
    }
    function editar($id = null)
    {
        $this->Plano->id = $id;
        if (!$id) {
            $this->Session->setFlash('No Existe el tipo de Plano');
            $this->redirect(array('action' => 'index'));
        }
        if (empty($this->data)) {
            $this->data = $this->Plano->read();
        } else {
            if ($this->Plano->save($this->data)) {
                $this->Session->setFlash('Se Guardo Correctamente el Plano');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error al guardar el Plano');
            }
        }

    }

    function insertarplano()
    {
        if (!empty($this->data)) {
            if ($this->Plano->save($this->data)) {
                $this->Session->setFlash('Se Guardo Correctamente!!!');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error al Guardar consulte con el Administrador de Sistema');
            }
        }

    }

}
